var gridList, pluginsGridList;
var moduleId;

$(document).ready(function () {
    gridList = new dataGrid("gridList");
    pluginsGridList = new dataGrid("pluginsGridList");
    init_Data();
});
//初始化数据
function init_Data(){
    gridList.load({"moduleState":1});
    pluginsGridList.load({'moduleId': -1,'pluginsState': "1"});
}
//刷新
function refreshData() {
    gridList.reload();
    pluginsGridList.reload();
}
//查询
function searchData() {
    gridList.reload();
    pluginsGridList.reload();
}

function reloadPluginsList(e) {
    moduleId = e.moduleId;
    moduleName = e.moduleName;
    $("#pluginsTitle").html("控件列表-<span style='color:red'>"+moduleName+"</span>");
    pluginsGridList.load({'moduleId': moduleId,'pluginsState': "1"});
}

function formatOper(e) {
    var row=e.record;
    var name = row.moduleName || '';
    var module = row.moduleId || '';

    var btn = "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:void(0)' lay-event='del' onClick='moduleMoveUp(\""+module+"\")' style='margin-left: 4px'><i class='layui-icon'>&#xe619;</i></a>"+
        "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' hideFocus='' href='javascript:void(0)' lay-event='del' onClick='moduleMoveDown(\""+module+"\")' style='margin-left: 4px'><i class='layui-icon'>&#xe61a;</i></a>" +
        "<a class='layui-btn layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='editModule(\"" + module + "\")'><i class='layui-icon'>&#xe642;</i>修改</a>" +
        "<a class='layui-btn layui-btn-danger layui-btn-xs' hideFocus='' href='javascript:void(0)' onClick='removeModule(\"" + module + "\",\"" + name + "\")'><i class='layui-icon'>&#xe640;</i>删除</span></a>";
    return btn;
}

function formatOper2(e) {
    var record = e.record;
    var pluginId = record.fpId;
    var nameCn = record.nameCn;

    var btns = "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' lay-event='move' onClick='pluginsMoveUp(\""+pluginId+"\")'><i class='layui-icon'>&#xe619;</i>上移</a>"+
        "<a class='layui-btn layui-btn-normal layui-btn-xs st_back2' lay-event='move' onClick='pluginsMoveDown(\""+pluginId+"\")'><i class='layui-icon'>&#xe61a;</i>下移</a>" +
        "<a class='layui-btn layui-btn-xs st_back1 hasRecord' lay-event='edit' onClick='editPlugins(\""+pluginId+"\")'><i class='layui-icon'>&#xe642;</i>修改</a>" +
        "<a class='layui-btn layui-btn-danger layui-btn-xs st_back3' lay-event='del' onClick='removePlugins(\""+pluginId+"\",\""+nameCn+"\")'><i class='layui-icon'>&#xe640;</i>删除</a>";
    return btns;
}

function addData() {
    layer.open({
        type: 2,
        title:"添加模块",
        area: ['750px', '350px'],
        fixed: false, //不固定
        maxmin: true,
        content: "../module/addModule.html?type=add",
        end: function () {
            gridList.reload();
        }
    });
}

function editModule(id) {
    layer.open({
        type: 2,
        area: ['750px', '350px'],
        fixed: false, //不固定
        maxmin: true,
        content: "../module/addModule.html?type=edit&id="+id,
        end: function () {
            gridList.reload();
        }
    });
}

function removeModule(id, title) {
    layer.confirm('确定要删除' + title + '么？', {
        btn: ['确定', '取消'] //按钮
    }, function () {
        $.post("/wf/module/update", {"moduleId": id,"moduleState":2}, function (data) {
            if (data.OK == "OK") {
                layer.msg('操作成功！', {
                    icon: 1,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            } else {
                layer.msg('操作失败！', {
                    icon: 2,
                    time: 1000, //1秒关闭（如果不配置，默认是3秒）
                    shade: 0.3
                }, function () {
                    gridList.reload();
                })
            }
        });
    }, function () {
    });
}

//上移
function moduleMoveUp(e) {
    ActHttpRequest("/wf/module/moveUp",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else {
            layer.msg("操作出错", {icon: 2, time:1000, shade: 0.3});
        }
        gridList.reload();
    });
}

//下移
function moduleMoveDown(e) {
    ActHttpRequest("/wf/module/moveDown",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else {
            layer.msg("操作出错", {icon: 2, time:1000, shade: 0.3});
        }
        gridList.reload();
    });
}

//模块回收站
function recycleBin() {
    layer.open({
        type: 2,
        title: '模块回收站',
        area: ['1000px', '610px'],
        fixed: false, //不固定
        maxmin: true,
        content: "../module/moduleRecycle.html",
        end: function () {
            gridList.reload();
        }
    });
}

//修改控件
function editPlugins(id) {
    parent.layer.open({
        type: 2
        ,title: '修改控件'
        ,area: ['700px', '400px']
        ,shadeClose: true
        ,maxmin: false
        ,content: 'base/workFlow/module/pluginEdit.html?type=edit&id='+id
        ,zIndex: layer.zIndex
        ,end:function () {
            pluginsGridList.reload();
        }
    });
}

//删除
function removePlugins(id,name) {
    parent.layer.confirm('确定要删除'+name+'控件么?', {
        btn: ['确定','取消'] //按钮
    }, function(index){
        ActHttpRequest("/wf/form/plugins/update",{"fpId":id,"pluginsState":"2"},function (res) {
            if(res.OK == "OK"){
                pluginsGridList.reload();
                parent.layer.msg('操作成功', {icon: 1,  shade: 0.3});
            }else{
                parent.layer.msg('操作失败', {icon: 1,  shade: 0.3});
            }
        });
        parent.layer.close(index);
    });
}

//上移
function pluginsMoveUp(e) {
    ActHttpRequest("/wf/form/plugins/moveUp",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else{
            layer.msg("操作失败", {icon: 2, time:1000, shade: 0.3});
        }
        pluginsGridList.reload();
    });
}

//下移
function pluginsMoveDown(e) {
    ActHttpRequest("/wf/form/plugins/moveDown",{"id":e},function (data) {
        if(data.OK == "OK"){
            layer.msg(data.msg, {icon: 1, time:1000, shade: 0.3});
        }else{
            layer.msg("操作失败", {icon: 2, time:1000, shade: 0.3});
        }
        pluginsGridList.reload();
    });
}

//导入
function inputData() {
    if(moduleId == "" || moduleId == undefined ){
        parent.layer.alert("请先选择模块！",{icon: 7, title:'提示'});
    }else {
        if ($(".hasRecord").length == 0) {
            inputDbInfo()
        } else {
            parent.layer.confirm('是否覆盖该模块下的所有控件？', {icon: 7, title:'提示'}, function(index){
                inputDbInfo();
                parent.layer.close(index);
            })
        }
    }
}

function inputDbInfo() {
    $.ajax({
        url:"/wf/form/plugins/inputDbInfo",
        data:{"moduleId": moduleId},
        type:"post",
        async:false,
        dataType:"json",
        success:function(data) {
            console.log(data)
            if(data.OK == "OK"){
                if (data.msg == "Error") {
                    parent.layer.alert("导入失败，请检查【模块代号】是否输入正确。",{icon:2});
                }else if(data.msg == "OK") {
                    parent.layer.alert("导入控件成功")
                }
            }else{
                parent.layer.alert("导入失败，请检查【模块代号】是否输入正确。",{icon:2});
            }

            pluginsGridList.reload({'moduleId': moduleId,'pluginsState': "1"});
        }
    });
}
